package com.junjiao.util.java.email;

import java.io.File;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

import com.junjiao.util.java.PoxyUtil;
import com.junjiao.util.java.StringUtil;

/**
 * @author wgz
 * 
 */
public class SendMail {

	String host;
	String userName;
	String password;
	Session session;
	int sslPort;
	public final static int E_PLAIN = 1;
	public final static int E_HTML = 2;
	private final static String EN_CODE = "utf-8";

	/**
	 * @param host
	 * @param userName
	 * @param password
	 * @param sslPort
	 * @throws Exception
	 */
	public SendMail(String host, String userName, String password, int sslPort)
			throws Exception {

		this.host = host;
		this.userName = userName;
		this.password = password;
		this.sslPort = sslPort;
		setEmailSession();
	}

	/**
	 * @param mainSubject
	 *            邮件主题
	 * @param to
	 *            接收人
	 * @param copy
	 *            抄送人
	 * @param backCopy
	 *            密送人
	 * @param from
	 *            发件人
	 * @param content
	 *            邮件内容
	 * @param attachfiles
	 *            邮件附件
	 * @param type
	 *            邮件类型
	 * @return
	 * @throws Exception
	 */
	private MimeMessage initMimeMessage(String mainSubject, String[] to,
			String[] copy, String[] backCopy, String from, String content,
			File[] attachfiles, int type) throws Exception {

		if (this.session == null) {
			throw new Exception("session is null!");
		}

		try {

			MimeMessage message = new MimeMessage(session);
			Multipart mp = new MimeMultipart();
			File file = null;

			if (to != null) {
				Address[] address = new Address[to.length];
				for (int i = 0; i < to.length; i++) {
					address[i] = new InternetAddress(to[i]);
				}
				message.setRecipients(Message.RecipientType.TO, address);
			}

			if (copy != null) {
				Address[] address = new Address[copy.length];
				for (int i = 0; i < copy.length; i++) {
					address[i] = new InternetAddress(copy[i]);
				}
				message.setRecipients(Message.RecipientType.CC, address);
			}

			if (backCopy != null) {
				Address[] address = new Address[backCopy.length];
				for (int i = 0; i < backCopy.length; i++) {
					address[i] = new InternetAddress(backCopy[i]);
				}
				message.setRecipients(Message.RecipientType.BCC, address);
			}

			message.setFrom(new InternetAddress(from));
			message.setSubject(mainSubject);

			if (attachfiles != null) {
				for (int i = 0; i < attachfiles.length; i++) {
					file = attachfiles[i];
					MimeBodyPart mbp = new MimeBodyPart();
					FileDataSource fds = new FileDataSource(file);
					mbp.setDataHandler(new DataHandler(fds));
					mbp.setFileName(fds.getName());
					mp.addBodyPart(mbp);

				}
			}

			MimeBodyPart contentPart = null;
			switch (type) {
			case 1:
				if (!"".equals(content)) {
					contentPart = new MimeBodyPart();
					contentPart.setText(content, EN_CODE);
				}
				;
				break;

			case 2:
				if (!"".equals(content)) {
					contentPart = new MimeBodyPart();
					contentPart.setContent(content, "text/html;charset="
							+ EN_CODE);
				}
				;
				break;

			default:
				if (!"".equals(content)) {
					contentPart = new MimeBodyPart();
					contentPart.setText(content, EN_CODE);
				}
				;
			}

			if (contentPart != null) {
				mp.addBodyPart(contentPart);
			}

			message.setContent(mp);
			message.setSentDate(new Date());

			return message;

		} catch (Exception e) {
			throw new Exception(e.getCause());
		}
	}

	/**
	 * @param mainSubject
	 *            邮件主题
	 * @param to
	 *            接收人
	 * @param from
	 *            发件人
	 * @param content
	 *            发送邮件内容
	 * @param attachfiles
	 *            发送附件
	 * @param type
	 *            邮件类型
	 * @return
	 * @throws Exception
	 */
	public MimeMessage createMimeMessage(String mainSubject, String[] to,
			String from, String content, File[] attachfiles, int type)
			throws Exception {

		if (to.length <= 0) {
			throw new NullPointerException("receiver is empty!");
		}

		if (!"".equals(StringUtil.nullToStr(from))) {
			throw new NullPointerException("receiver is empty!");
		}

		return initMimeMessage(mainSubject, to, null, null, from, content,
				attachfiles, type);

	}

	/**
	 * @param mainSubject
	 *            邮件主题
	 * @param to
	 *            接收人
	 * @param from
	 *            发件人
	 * @param content
	 *            发送邮件内容
	 * @param attachfiles
	 *            发送附件
	 * @return
	 * @throws Exception
	 */
	public MimeMessage createMimeMessage(String mainSubject, String[] to,
			String from, String content, File[] attachfiles) throws Exception {

		if (to.length <= 0) {
			throw new NullPointerException("receiver is empty!");
		}

		if ("".equals(StringUtil.nullToStr(from))) {
			throw new NullPointerException("cc receiver is empty!");
		}

		return initMimeMessage(mainSubject, to, null, null, from, content,
				attachfiles, E_PLAIN);

	}

	/**
	 * @param mainSubject
	 *            邮件主题
	 * @param to
	 *            接收人
	 * @param from
	 *            发件人
	 * @param content
	 *            发送邮件内容
	 * @return
	 * @throws Exception
	 */
	public MimeMessage createMimeMessage(String mainSubject, String[] to,
			String from, String content) throws Exception {

		if (to.length <= 0) {
			throw new NullPointerException("receiver is empty!");
		}

		if ("".equals(StringUtil.nullToStr(from))) {
			throw new NullPointerException("receiver is empty!");
		}

		return initMimeMessage(mainSubject, to, null, null, from, content,
				null, E_PLAIN);

	}

	/**
	 * @param mainSubject
	 *            邮件主题
	 * @param to
	 *            接收人
	 * @param from
	 *            发件人
	 * @param content
	 *            发送邮件内容
	 * @param type
	 *            发送邮件类型
	 * @return
	 * @throws Exception
	 */
	public MimeMessage createMimeMessage(String mainSubject, String[] to,
			String from, String content, int type) throws Exception {

		if (to.length <= 0) {
			throw new NullPointerException("receiver is empty!");
		}

		if ("".equals(StringUtil.nullToStr(from))) {
			throw new NullPointerException("receiver is empty!");
		}

		return initMimeMessage(mainSubject, to, null, null, from, content,
				null, type);

	}

	/**
	 * @param mainSubject
	 *            邮件主题
	 * @param to
	 *            接收人
	 * @param copy
	 *            抄送人
	 * @param from
	 *            发件人
	 * @param content
	 *            发送邮件内容
	 * @return
	 * @throws Exception
	 */
	public MimeMessage createMimeMessage(String mainSubject, String[] to,
			String[] copy, String from, String content) throws Exception {

		if (to.length <= 0) {
			throw new NullPointerException("receiver is empty!");
		}

		if (copy.length <= 0) {
			throw new NullPointerException("copy is empty!");
		}

		if ("".equals(StringUtil.nullToStr(from))) {
			throw new NullPointerException("receiver is empty!");
		}

		return initMimeMessage(mainSubject, to, copy, null, from, content,
				null, E_PLAIN);

	}

	/**
	 * @param mainSubject
	 *            邮件主题
	 * @param to
	 *            接收人
	 * @param copy
	 *            抄送人
	 * @param from
	 *            发件人
	 * @param content
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public MimeMessage createMimeMessage(String mainSubject, String[] to,
			String[] copy, String from, String content, int type)
			throws Exception {

		if (to.length <= 0) {
			throw new NullPointerException("receiver is empty!");
		}

		if (copy.length <= 0) {
			throw new NullPointerException("copy is empty!");
		}

		if ("".equals(StringUtil.nullToStr(from))) {
			throw new NullPointerException("receiver is empty!");
		}

		return initMimeMessage(mainSubject, to, copy, null, from, content,
				null, type);

	}

	/**
	 * @param mainSubject
	 * @param to
	 * @param copy
	 * @param backcopy
	 * @param from
	 * @param content
	 * @return
	 * @throws Exception
	 */
	public MimeMessage createMimeMessage(String mainSubject, String[] to,
			String[] copy, String[] backcopy, String from, String content)
			throws Exception {

		if (to.length <= 0) {
			throw new NullPointerException("receiver is empty!");
		}

		if (copy.length <= 0) {
			throw new NullPointerException("copy is empty!");
		}

		if ("".equals(StringUtil.nullToStr(from))) {
			throw new NullPointerException("receiver is empty!");
		}

		return initMimeMessage(mainSubject, to, copy, backcopy, from, content,
				null, E_PLAIN);
	}

	/**
	 * @param mainSubject
	 * @param to
	 * @param copy
	 * @param backcopy
	 * @param from
	 * @param content
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public MimeMessage createMimeMessage(String mainSubject, String[] to,
			String[] copy, String[] backcopy, String from, String content,
			int type) throws Exception {

		if (to.length <= 0) {
			throw new NullPointerException("receiver is empty!");
		}

		if (copy.length <= 0) {
			throw new NullPointerException("copy is empty!");
		}

		if ("".equals(StringUtil.nullToStr(from))) {
			throw new NullPointerException("receiver is empty!");
		}

		return initMimeMessage(mainSubject, to, copy, backcopy, from, content,
				null, type);
	}

	/**
	 * @param mainSubject
	 *            邮件主题
	 * @param to
	 *            接收人
	 * @param copy
	 *            抄送人
	 * @param backCopy
	 *            密送人
	 * @param from
	 *            发件人
	 * @param content
	 *            邮件内容
	 * @param attachfiles
	 *            邮件附件
	 * @param type
	 *            邮件类型
	 * @return
	 * @throws Exception
	 */
	public MimeMessage createMimeMessage(String mainSubject, String[] to,
			String[] copy, String[] backcopy, String from, File[] attachfiles,
			String content) throws Exception {

		if (to.length <= 0) {
			throw new NullPointerException("receiver is empty!");
		}

		if (copy.length <= 0) {
			throw new NullPointerException("copy is empty!");
		}

		if ("".equals(StringUtil.nullToStr(from))) {
			throw new NullPointerException("receiver is empty!");
		}

		return initMimeMessage(mainSubject, to, copy, backcopy, from, content,
				attachfiles, E_PLAIN);
	}

	/**
	 * @param mainSubject
	 *            邮件主题
	 * @param to
	 *            接收人
	 * @param copy
	 *            抄送人
	 * @param backCopy
	 *            密送人
	 * @param from
	 *            发件人
	 * @param content
	 *            邮件内容
	 * @param attachfiles
	 *            邮件附件
	 * @param type
	 *            邮件类型
	 * @return
	 * @throws Exception
	 */
	public MimeMessage createMimeMessage(String mainSubject, String[] to,
			String[] copy, String[] backcopy, String from, File[] attachfiles,
			String content, int type) throws Exception {

		if (to.length <= 0) {
			throw new NullPointerException("receiver is empty!");
		}

		if (copy.length <= 0) {
			throw new NullPointerException("copy is empty!");
		}

		if ("".equals(StringUtil.nullToStr(from))) {
			throw new NullPointerException("receiver is empty!");
		}

		return initMimeMessage(mainSubject, to, copy, backcopy, from, content,
				attachfiles, type);
	}

	/**
	 * @param message
	 * @return
	 */
	public boolean send(final MimeMessage message) {

		try {
			Transport.send(message);
			System.out.println("mail send successful!!");

		} catch (MessagingException mex) {

			mex.printStackTrace();
			Exception ex = null;
			if ((ex = mex.getNextException()) != null) {
				ex.printStackTrace();
			}
			return false;

		} catch (Exception e) {

			e.printStackTrace();
			return false;
		}

		return true;
	}

	private void setEmailSession() throws Exception {

		try {
			if ("".equals(StringUtil.nullToStr(host))) {
				throw new Exception("host is null!");
			}

			if ("".equals(StringUtil.nullToStr(userName))) {
				throw new Exception("userName is null!");
			}

			if ("".equals(StringUtil.nullToStr(password))) {
				throw new Exception("password is null!");
			}

			this.session = EmailSsessionFactory.getSession(this.host,
					this.sslPort, this.userName, this.password);

		} catch (Exception e) {
			throw new Exception(e.getCause());
		}
	}

	public static void main(String[] args) {

		PoxyUtil.addPoxy();

		String host = "mail.besttone.com.cn";
		String userName = "jiaojun";
		String password = "15991033667";

		String mainSubject = "邮件标题";
		String to[] = new String[] { "zhanggaobin@besttone.com.cn",
				"junjiao.j@gmail.com" };
		String from = "junjiao.j@gmail.com";
		String content = "这是邮件的内容!";

		try {

			SendMail sendmail = new SendMail(host, userName, password, 0);
			MimeMessage message = sendmail.createMimeMessage(mainSubject, to,
					from, content);
			sendmail.send(message);

			EmailTools.newInstance().sendSimpleMail(content);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
